package com.zhenhao.xsq.ui.activity.login;

import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;

import com.orhanobut.logger.Logger;
import com.zhenhao.xsq.R;
import com.zhenhao.xsq.application.AppConfig;
import com.zhenhao.xsq.base.BaseActivity;
import com.zhenhao.xsq.contain.Contains;
import com.zhenhao.xsq.ui.activity.home.HomeActivity;
import com.zhenhao.xsq.ui.activity.login.component.DaggerSplashComponent;
import com.zhenhao.xsq.ui.activity.login.contract.SplashContract;
import com.zhenhao.xsq.ui.activity.login.module.SplashModule;
import com.zhenhao.xsq.ui.activity.login.presenter.SplashPresenter;
import com.zhenhao.xsq.utils.SpSaveUtils;
import com.zhenhao.xsq.utils.SpUtil;
import com.zhenhao.xsq.view.BaseDialog;
import com.zhenhao.xsq.view.PrivateHintDialog;

import javax.inject.Inject;

import butterknife.ButterKnife;
import me.jessyan.autosize.AutoSizeCompat;

import static com.zhenhao.xsq.contain.Contains.toType;


/**
 * @author xlei
 * @Package com.example.rxdemo.ui.activity.login
 * @Description: $description
 * @date 2019/01/07 10:39:32
 */

public class SplashActivity extends BaseActivity implements SplashContract.View {

    @Inject
    SplashPresenter mPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initView() {
        needFront = true;
        setContentView(R.layout.activity_splash);
        ButterKnife.bind(this);
//        getSupportActionBar().setDisplayHomeAsUpEnabled(false);
        autolayout.setVisibility(View.GONE);

    }

    @Override
    protected void initData() {


        int anInt = SpUtil.getInt(this, Contains.SP_APP_PRIVATE, 0);

        // 获取隐私政策签署状态
        boolean sign = false;

        if (anInt == 0) {
            sign = false;
        } else {
            sign = true;
        }

        if (sign) {
            agreePrvite();
        } else {
            // 没签，等签署之后再调用registerPush()
            showHintDialog();
        }
    }

    public void agreePrvite() {

        mPresenter.skipActivity(toType);
    }

    private PrivateHintDialog privateHintDialog;

    private void showHintDialog() {
        privateHintDialog = new PrivateHintDialog(this,
                new PrivateHintDialog.onPrivateHintDialogSelect() {
            @Override
            public void onPrivateHintDialogSelect(String str, int position) {
                Bundle bundle = new Bundle();
                switch (position) {
                    case 0:
                        //用户协议
//                        bundle.putString("name", "平台协议");
//                        bundle.putString("address", API.PINGTAI_XIEYI);
//                         startActivity(WebSatisficingActivity.class, bundle);
                        break;
                    case 1:
//                        startActivty(YingsiActivity.class);
//                        bundle.putString("name", "隐私政策");
//                        bundle.putString("address", API.YINSI_ZHENGCE);
//                        startActivity(WebSatisficingActivity.class, bundle);
                        break;
                    case 2:
                        finish();
                        break;
                    case 3:
                        privateHintDialog.dismiss();
//                        showPermissionsDialog();
                        SpUtil.putInt(SplashActivity.this, Contains.SP_APP_PRIVATE, 1);
                        mPresenter.skipActivity(toType);
                        break;
                }
            }
        });
        privateHintDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);//代码中取消标题栏
        privateHintDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        privateHintDialog.setCancelable(false);
        privateHintDialog.show();
    }

    private void showPermissionsDialog() {
        BaseDialog baseDialog = new BaseDialog(this);
        baseDialog.setSure("同意");
        baseDialog.setCancel("不同意");
        baseDialog.setTitle("权限申请");
        baseDialog.setContent("为了您更好的使用本APP，我们需要申请您的手机权限");
        baseDialog.setCancelListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                baseDialog.dismiss();
                mPresenter.skipActivity(toType);
            }
        });
        baseDialog.setSureListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                baseDialog.dismiss();
                SpUtil.putInt(SplashActivity.this, Contains.SP_APP_PRIVATE, 1);
//                mPresenter.getPermission(permissions);

            }
        });
        baseDialog.show();
    }


    @Override
    public void toLogin() {
        Logger.i("跳转到登录界面");
        if (TextUtils.isEmpty(SpSaveUtils.getAccessToken())) {
            startActivty(LoginActivity.class);
        } else {
            startActivty(HomeActivity.class);
        }
        finish();
    }

    @Override
    public void toHome() {
        Logger.i("跳转到首页");
        startActivty(HomeActivity.class);
        finish();
    }

    @Override
    public void toGuide() {
        Logger.i("跳转到欢迎界面");
        /*startActivty(GuideActivity.class);
        finish();*/
    }

    @Override
    protected void setupActivityComponent() {
        DaggerSplashComponent
                .builder()
                .appComponent(((AppConfig) getApplication()).getApplicationComponent())
                .splashModule(new SplashModule(this))
                .build()
                .inject(this);
    }

    @Override
    public void setPresenter(SplashContract.SplashContractPresenter presenter) {
        mPresenter = (SplashPresenter) presenter;
    }

    @Override
    public void showProgressDialog() {
        progressDialog.show();
    }

    @Override
    public void closeProgressDialog() {
        progressDialog.hide();
    }

    @Override
    public Resources getResources() {
        //需要升级到 v1.1.2 及以上版本才能使用 AutoSizeCompat
        AutoSizeCompat.autoConvertDensityOfGlobal((super.getResources()));//如果没有自定义需求用这个方法
        AutoSizeCompat.autoConvertDensity((super.getResources()), 375, true);//如果有自定义需求就用这个方法
        return super.getResources();
    }

}